package com.calciominoBeta.Adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.calciominoBeta.R;
import com.calciominoBeta.Custom.ImageDaetailCustomSize;
import com.calciominoBeta.Entity.InfoOj;
import com.calciominoBeta.Util.Util;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

public class Video_Adapter extends BaseAdapter {
	public static final int HOME = 1;
	public static final int ARTICLES = 2;
	public static final int BREVES = 3;
	public static final int VIDEO = 4;
	private ArrayList<InfoOj> datas;
	private LayoutInflater inflater;

	private ImageLoader imageLoader = ImageLoader.getInstance();

	private DisplayImageOptions options;

	private IClickItemVideo clickItemVideo;
	private Context context;

	public Video_Adapter(Context context, ImageLoader imageLoader,
			DisplayImageOptions options) {
		datas = new ArrayList<InfoOj>();
		inflater = LayoutInflater.from(context);

		this.context = context;

		this.imageLoader = imageLoader;
		this.options = options;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return datas.size();
	}

	@Override
	public InfoOj getItem(int position) {
		// TODO Auto-generated method stub
		return datas.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public void addData(InfoOj InfoVideoTest) {
		datas.add(InfoVideoTest);
		notifyDataSetChanged();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.custom_accueil_adapter,
					null);

			ViewHolder holder = new ViewHolder();
			holder.c_Avatar = (ImageDaetailCustomSize) convertView
					.findViewById(R.id.img_detail);
			holder.c_tv_date = (TextView) convertView
					.findViewById(R.id.tv_date_text);
			
			holder.c_tv_type = (TextView) convertView
					.findViewById(R.id.tv_type);
			
			holder.c_tv_title = (TextView) convertView
					.findViewById(R.id.tv_title);
		
			holder.c_llIndex = (LinearLayout) convertView
					.findViewById(R.id.llIndex);

			convertView.setTag(holder);

		}
		final InfoOj infoDetai = datas.get(position);
		if (infoDetai != null) {
			ViewHolder holder = (ViewHolder) convertView.getTag();

			// Debug.debug("Link Image : " + infoVideo.getvAvatar());

			imageLoader.displayImage(infoDetai.get(InfoOj.IMAGE),
					holder.c_Avatar, options, new ImageLoadingListener() {

						@Override
						public void onLoadingStarted(String imageUri, View view) {
						}

						@Override
						public void onLoadingFailed(String imageUri, View view,
								FailReason failReason) {
						}

						@Override
						public void onLoadingComplete(String imageUri,
								View view, Bitmap loadedImage) {
						}

						@Override
						public void onLoadingCancelled(String imageUri,
								View view) {
						}
					});

			holder.c_tv_type.setText(infoDetai.get(InfoOj.TYPE));
			holder.c_tv_date.setText(infoDetai.get(InfoOj.DATE_TEXT));
			holder.c_tv_title.setText(Html.fromHtml(infoDetai.get(InfoOj.TITLE)));
			holder.c_tv_title.setTypeface(Util.getTypeFont(context));
			holder.c_tv_type.setTypeface(Util.getTypeFont(context));
			holder.c_tv_date.setTypeface(Util.getTypeFont(context));
			if (infoDetai.get(InfoOj.TYPE).equals("breves")) {
				holder.c_tv_type.setText(context.getResources().getString(
						R.string.breves));
				holder.c_tv_type.setTextColor(context.getResources()
						.getColorStateList(R.color.breves));
			}
			if (infoDetai.get(InfoOj.TYPE).equals("videos")) {
				holder.c_tv_type.setText(context.getResources().getString(
						R.string.video_fr));
				holder.c_tv_type.setTextColor(context.getResources()
						.getColorStateList(R.color.video));
			}
			if (infoDetai.get(InfoOj.TYPE).equals("articles")) {
				holder.c_tv_type.setText("ARTICLE");
				holder.c_tv_type.setTextColor(context.getResources()
						.getColorStateList(R.color.articles));
			}

		}
		return convertView;
	}

	class ViewHolder {
		ImageDaetailCustomSize c_Avatar;
		TextView c_tv_type;
		TextView c_tv_date;
		TextView c_tv_title;
		LinearLayout c_llIndex;
	}

	public interface IClickItemVideo {
		void onclickItem(String c_id);
	}

	public ArrayList<InfoOj> getFull() {
		return datas;
	}

	public IClickItemVideo getClickItemVideo() {
		return clickItemVideo;
	}

	public void setClickItemVideo(IClickItemVideo clickItemVideo) {
		this.clickItemVideo = clickItemVideo;
	}

	public void setData(ArrayList<InfoOj> items) {
		if (items == null)
			return;
		datas = items;
		notifyDataSetChanged();

	}

}
